package com.example.yi.allWithYou.ui;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.example.yi.allWithYou.R;
import com.example.yi.allWithYou.api.ApiClientUsage;
import com.example.yi.allWithYou.api.DataHandler;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.json.JSONException;
import org.json.JSONObject;

public class MLoginActivity extends ActionBarActivity {

    Button loginbtn ;
    private ProgressDialog mLoginProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
      loginbtn = (Button)  findViewById(R.id.email_sign_in_button);
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("我正在登录");
                login("13231","2313");
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_mlogin, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void login( String username,  String password){




        RequestParams params = new RequestParams();
        params.add("userName",username);
        params.add("passWord",password);
        System.out.println("开始登录 " );
        Message msg =new Message();
        try{


          ApiClientUsage.userLogin(this, params, new DataHandler() {
              @Override
              public void getData(JSONObject jobj) {

                  System.out.println("MLoginActivity response println:  " + jobj);

              }
          }) ;



        }catch (JSONException e){
            msg.what = -1;
            msg.obj = e;
        }

//
//
//        if(mLoginProgressDialog == null) {
//            mLoginProgressDialog = new ProgressDialog(this);
//            mLoginProgressDialog.setCancelable(true);
//            mLoginProgressDialog.setCanceledOnTouchOutside(false);
//            mLoginProgressDialog.setMessage("登录中");
//        }
//
//        ApiClient.get("users/login", params, new JsonHttpResponseHandler() {
//
//            @Override
//            public void onStart() {
//                super.onStart();
//                mLoginProgressDialog.show();
//            }
//
//            //返回的数据必须是一个json对象
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, JSONObject responseString) {
////                super.onSuccess(statusCode, headers, responseString);
//                mLoginProgressDialog.hide();
//                System.out.println("response2:  " + responseString);
//            }
//            //当没有返回数据或者返回数据格式不对的时候都会调用这个方法
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                super.onFailure(statusCode, headers, responseString, throwable);
//                mLoginProgressDialog.hide();
//                System.out.println("response3:  " + responseString);
//            }
//        });

//
//        if(mLoginProgressDialog == null) {
//            mLoginProgressDialog = new ProgressDialog(this);
//            mLoginProgressDialog.setCancelable(true);
//            mLoginProgressDialog.setCanceledOnTouchOutside(false);
//            mLoginProgressDialog.setMessage("登录中");
//        }
//
//
//
//       //登录验证
//           new AsyncTask<Void,Void,Message>(){
//               @Override
//               protected Message doInBackground(Void... params) {
//                   System.out.println("用户名"+username+"\n密码："+password);
//                   mLoginProgressDialog.show();
//                   return null;
//               }
//
//               @Override
//               protected void onPreExecute() {
//                   super.onPreExecute();
//                   mLoginProgressDialog.show();
//
//               }
//
//               @Override
//               protected void onProgressUpdate(Void... values) {
//                   super.onProgressUpdate(values);
//                   System.out.println("用户名"+username+"\n密码："+password);
//               }
//
//               @Override
//               protected void onPostExecute(Message message) {
//                   super.onPostExecute(message);
//                   mLoginProgressDialog.show();
//               }
//           }.execute();
    }

    public RequestHandle loginAction(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
        return client.get(this, URL, headers, null, responseHandler);
    }

    public int getMLoginTitle() {
        return R.string.title_get_MLogin;
    }

}
